package com.cya.forumproject.services.impl;

import com.cya.forumproject.common.AppResult;
import com.cya.forumproject.common.ResultCode;
import com.cya.forumproject.dao.MessageMapper;
import com.cya.forumproject.exception.ApplicationException;
import com.cya.forumproject.model.Message;
import com.cya.forumproject.model.User;
import com.cya.forumproject.services.IMessageService;
import com.cya.forumproject.services.IUserService;
import com.cya.forumproject.utils.StringUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/9/26 22:14
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
@Api(tags = "私信接口")
public class MessageServiceImpl implements IMessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private IUserService userService;

    @Override
    public void create(Message message) {
            //非空
        if (message==null||message.getPostUserId()==null||message.getReceiveUserId()==null
        || StringUtil.isEmpty(message.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        检验接收者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if (user==null||user.getDeleteState()==1){
            throw new ApplicationException(ResultCode.FAILED_USER_NOT_EXISTS.toString());
        }
//        设置默认值
        message.setState((byte) 0);//表示未读
        message.setDeleteState((byte) 0);
        message.setCreateTime(new Date());
        message.setUpdateTime(new Date());

        int row = messageMapper.insertSelective(message);
        if (row!=1){
            log.warn(ResultCode.FAILED.toString()+", 受影响行数不等于1.");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        if (id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if (count==null){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));

        }

        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if (receiveUserId==null||receiveUserId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateStateById(Long id, byte state) {
        if (id==null||id<=0||state<0||state>2){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        Message message = messageMapper.selectByPrimaryKey(id);
        if (message==null||message.getDeleteState()==1){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        Message updateMessage=new Message();

        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
@Transactional
    @Override
    public void reply(Long replyId, Message message) {
        if (replyId==null||replyId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message exisMmessage = messageMapper.selectByPrimaryKey(replyId);
        if (exisMmessage==null||exisMmessage.getDeleteState()==1){
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
//        更新状态一回复
      updateStateById(replyId, (byte) 2);
//        将回复的内容插入库
        create(message);

    }
}